package com.ecoolex.union.pay.retailer.core.manager;

import com.ecoolex.framework.common.enums.ResultCode;
import com.ecoolex.framework.common.pojo.Page;
import com.ecoolex.framework.common.pojo.Pagination;
import com.ecoolex.framework.common.util.BizAssert;
import com.ecoolex.framework.common.util.Check;
import com.ecoolex.union.pay.retailer.core.dao.UnionPayAutoReceiveCouponConfigDAO;
import com.ecoolex.union.pay.retailer.entity.UnionPayAutoReceiveCouponConfig;
import com.ecoolex.union.pay.retailer.entity.UnionPayAutoReceiveCouponConfig.Column;
import com.ecoolex.union.pay.retailer.entity.UnionPayAutoReceiveCouponConfigExample;
import com.ecoolex.union.pay.retailer.entity.UnionPayAutoReceiveCouponConfigExample.Criteria;

import java.time.Instant;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UnionPayAutoReceiveCouponConfigManager {
	
    @Autowired
    private UnionPayAutoReceiveCouponConfigDAO dao;
    
    public UnionPayAutoReceiveCouponConfig get(Integer configId) {
		return dao.selectByPrimaryKeyWithLogicalDelete(configId, UnionPayAutoReceiveCouponConfig.NOT_DELETED);
	}

	public Pagination<UnionPayAutoReceiveCouponConfig> query(String channelId, String couponCode, Page page) {
		UnionPayAutoReceiveCouponConfigExample example = new UnionPayAutoReceiveCouponConfigExample();
		Criteria criteria = example.createCriteria();
		if(Check.notNull(channelId)) {
			criteria.andChannelIdEqualTo(channelId);
		}
		if(Check.notNull(couponCode)) {
			criteria.andCouponCodeEqualTo(couponCode);
		}
		criteria.andDeletedEqualTo(UnionPayAutoReceiveCouponConfig.NOT_DELETED);
		example.setOffset(page.getFirstIndex());
		example.setRows(page.getPageSize());
		example.orderBy(Column.createTime.desc());
		return Pagination.create(page, 
			dao.countByExample(example),
			() -> dao.selectByExample(example));
	}
	
	private UnionPayAutoReceiveCouponConfig getByChannelIdAndCouponCode(String channelId, String couponCode) {
		UnionPayAutoReceiveCouponConfigExample example = new UnionPayAutoReceiveCouponConfigExample();
		Criteria criteria = example.createCriteria();
		criteria.andChannelIdEqualTo(channelId).andCouponCodeEqualTo(couponCode).andDeletedEqualTo(UnionPayAutoReceiveCouponConfig.NOT_DELETED);
		return dao.selectOneByExample(example);
	}
	
	public List<UnionPayAutoReceiveCouponConfig> queryByChannelId(String channelId) {
		UnionPayAutoReceiveCouponConfigExample example = new UnionPayAutoReceiveCouponConfigExample();
		Criteria criteria = example.createCriteria();
		criteria.andChannelIdEqualTo(channelId).andDeletedEqualTo(UnionPayAutoReceiveCouponConfig.NOT_DELETED);
		return dao.selectByExample(example);
	}

	public UnionPayAutoReceiveCouponConfig add(UnionPayAutoReceiveCouponConfig add, String createUser){
		UnionPayAutoReceiveCouponConfig config = getByChannelIdAndCouponCode(add.getChannelId(),add.getCouponCode());
		BizAssert.isTrue(Check.isNull(config),ResultCode.BIZ.build(1), "配置数据重复");
		add.setDeleted(UnionPayAutoReceiveCouponConfig.NOT_DELETED);
		add.setCreateUser(createUser);
		add.setCreateTime(Date.from(Instant.now()));
		add.setUpdateUser(createUser);
		add.setUpdateTime(Date.from(Instant.now()));
		dao.insert(add);
		return add;
	}

	public UnionPayAutoReceiveCouponConfig edit(UnionPayAutoReceiveCouponConfig edit, String updateUser){
		UnionPayAutoReceiveCouponConfig config = getByChannelIdAndCouponCode(edit.getChannelId(),edit.getCouponCode());
		BizAssert.isTrue(Check.isNull(config),ResultCode.BIZ.build(1), "配置数据重复");
		edit.setUpdateUser(updateUser);
		edit.setUpdateTime(Date.from(Instant.now()));
		dao.updateByPrimaryKeySelective(edit, Column.excludes(Column.deleted,Column.createUser,Column.createTime));
		return edit;
	}

	public Boolean delete(UnionPayAutoReceiveCouponConfig edit, String updateUser){
		edit.setDeleted(UnionPayAutoReceiveCouponConfig.IS_DELETED);
		edit.setUpdateUser(updateUser);
		edit.setUpdateTime(Date.from(Instant.now()));
		int row = dao.updateByPrimaryKeySelective(edit, Column.deleted,Column.updateUser,Column.updateTime);
		return row == 1;
	}
}